package com.bazhua.bazo.controller;


import com.bazhua.bazo.common.utils.JwtUtil;
import com.bazhua.bazo.repository.entity.ShopInfo;
import com.bazhua.bazo.repository.model.SaveShopModel;
import com.bazhua.bazo.repository.model.SearchShopModel;
import com.bazhua.bazo.repository.model.ShopStatus;
import com.bazhua.bazo.repository.model.UpdateShopInfoModel;
import com.bazhua.bazo.repository.model.result.BaseResultCode;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.IShopInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "商户接口")
@RestController
@RequestMapping("/api/shop")
public class ShopInfoController {

    @Autowired
    private IShopInfoService shopInfoService;


    @ApiOperation("注册商户")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult registerShop(@RequestBody SaveShopModel model, @RequestHeader String token) {
        String openId = JwtUtil.getWxOpenIdByToken(token);
        Long userId = JwtUtil.getUserIdByToken(token);
        model.setShopAdmin(userId);
        model.setShopAdminOpenid(openId);
        return shopInfoService.registerShop(model);
    }

    @ApiOperation("修改商户信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult registerShop(@RequestBody UpdateShopInfoModel model) {
        return shopInfoService.updateShop(model);
    }

    @ApiOperation("审核商户")
    @RequestMapping(value = "/approve", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult approveShop(@RequestParam Long shopId,Integer shopStatus) {
        return shopInfoService.UpdateShopStatus(shopId, shopStatus);
    }

    @ApiOperation("禁用商户")
    @RequestMapping(value = "/forbid", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult forbidShop(@RequestParam Long shopId) {
        return shopInfoService.UpdateShopStatus(shopId, ShopStatus.Forbid);
    }

    @ApiOperation("启用商户")
    @RequestMapping(value = "/enable", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult enableShop(@RequestParam Long shopId) {
        return shopInfoService.UpdateShopStatus(shopId, ShopStatus.Approved);
    }

    @ApiOperation("分页查询商户列表")
    @RequestMapping(value = "/Page", method = RequestMethod.POST)
    @ResponseBody
    public PageListResult getShopPage(@RequestBody SearchShopModel model) {
        return shopInfoService.SearchShop(model);
    }

    @ApiOperation("分页查询代理业务员或者代理商推荐的商户列表")
    @RequestMapping(value = "/PageByAgentId", method = RequestMethod.POST)
    @ResponseBody
    public PageListResult getShopPageByAgentId(@RequestBody SearchShopModel model, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return shopInfoService.SearchShopByAgentId(model, userId);
    }

    @ApiOperation("获取单个商户明细")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getShop(@RequestParam Long shopId) {
        return shopInfoService.GetShopById(shopId);
    }


    @ApiOperation("根据openid获取单个商户明细")
    @RequestMapping(value = "/getByOpenId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getShopByOpenId(HttpServletRequest request) {
        String token = request.getHeader("token");
        String openId = JwtUtil.getWxOpenIdByToken(token);
        return shopInfoService.GetShopByOpenId(openId);
    }


    @ApiOperation("检查用户是否拥有商户")
    @RequestMapping(value = "/checkShopByOpenId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult checkShopByOpenId(@RequestHeader String token){
        String openId = JwtUtil.getWxOpenIdByToken(token);
        return shopInfoService.CheckShopByOpenId(openId);
    }
}
